package com.xbongbong.formula.qlexpress.operator.logic;

import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import com.ql.util.express.Operator;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wufeng
 * @date: 2018/7/5 15:22
 *
 * @desrcption: 对参数逻辑值求反；就是把括号里为true的值变成false，括号里为false的值变为true。
 */
public class OperatorXor extends Operator {

    public OperatorXor(String name){
        this.name = name;
    }

    public OperatorXor(String aliasName, String aName, String errorInfo){
        this.aliasName = aliasName;
        this.name = aName;
        this.errorInfo = errorInfo;
    }

    /**
     * @param list
     * list[0] 条件1
     * list[1] 条件2
     * @return 异或的含义是，两个值相同，返回false，两个值不同，返回true。
     * @throws Exception
     */
    @Override
    public Object executeInner(Object[] list) throws Exception {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        List<Boolean> booleanList = new ArrayList<>();

        for (int i = 0; i < list.length; i++) {
            Object r = runner.execute(list[i].toString(), context, null, true, false);
            booleanList.add( (Boolean) r );
        }
        if (booleanList.get(0) && !booleanList.get(1)) {
            return true;
        } else if (!booleanList.get(0) && booleanList.get(1)) {
            return true;
        }
        return false ;
    }
}

